using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace ZombieHigh2.Menus
{
    public class AnimatedLocker
    {
        Model locker;
        Model lockerDoor;
        Texture2D lockerTexture;
        Texture2D lockerDoorTexture;
        Vector3 position;
        Vector3 doorOffset;
        TextSwirl textSwirl;
        float rotationY;

        const int STATE_CLOSED = 0;
        const int STATE_CLOSING = 1;
        const int STATE_OPENING = 2;
        const int STATE_OPEN = 3;
        const int STATE_TEXT_IN_TRANSITION = 4;
        const int STATE_TEXT_NOT_IN_TRANSITION = 5;
        const int STATE_TEXT_MOVING_TO_START = 6;
        const int STATE_TEXT_MOVING_TO_END = 7;

        int currentState;
        int textState;

        public Boolean IsClosed
        {
            get { return currentState == STATE_CLOSED; }
        }

        public Boolean IsOpen
        {
            get { return currentState == STATE_OPEN; }
        }

        public Boolean IsClosing
        {
            get { return currentState == STATE_CLOSING; }
        }

        public Boolean IsOpening
        {
            get { return currentState == STATE_OPENING; }
        }

        public AnimatedLocker(Vector3 position)
        {
            this.position = position;
            doorOffset = new Vector3(-8, 0, 6);
            currentState = STATE_CLOSED;
            textState = STATE_TEXT_NOT_IN_TRANSITION;
            
        }

        public void LoadModels(ContentManager content, String textAsset)
        {
            locker = content.Load<Model>(@"Data\Models\model_locker");
            lockerTexture = content.Load<Texture2D>(@"Data\Models\uv_locker");
            lockerDoor = content.Load<Model>(@"Data\Models\model_locker_door");
            lockerDoorTexture = content.Load<Texture2D>(@"Data\Models\uv_locker_door");

            textSwirl = new TextSwirl(content.Load<Model>(textAsset),
                content.Load<Texture2D>(@"Data\Models\uv_new"),
                position + new Vector3(0,10,0),
                new Vector3(28, 25, 40),
                0.5f);
        }

        public void Update(float elapsedTime)
        {
            textSwirl.Update(elapsedTime);

            if (textState == STATE_TEXT_NOT_IN_TRANSITION)
            {
                switch (currentState)
                {
                    case STATE_OPENING:
                        rotationY -= 10f;
                        if (rotationY < -135f)
                        {
                            rotationY = -135f;
                            textState = STATE_TEXT_MOVING_TO_END;
                            textSwirl.MoveToEnd();
                            //currentState = STATE_OPEN;
                        }
                        break;

                    case STATE_CLOSING:
                        rotationY += 10f;
                        if (rotationY > 0f)
                        {
                            rotationY = 0f;
                            currentState = STATE_CLOSED;
                        }
                        break;

                }
            }
            else
            {
                if (textSwirl.IsAtEnd && currentState == STATE_OPENING)
                {
                    currentState = STATE_OPEN;
                    textState = STATE_TEXT_NOT_IN_TRANSITION;
                }
                else if (textSwirl.IsAtStart && currentState == STATE_CLOSING)
                {
                    textState = STATE_TEXT_NOT_IN_TRANSITION;
                }
            }
        }

        public void Open()
        {
            if (currentState == STATE_CLOSED)
            {
                currentState = STATE_OPENING;
            }
        }

        public void Close()
        {
            if (currentState == STATE_OPEN)
            {
                textState = STATE_TEXT_MOVING_TO_START;
                textSwirl.MoveToStart();
                currentState = STATE_CLOSING;
            }
        }

        public void Draw(Matrix viewMatrix, Matrix projectionMatrix)
        {
            DrawLocker(viewMatrix, projectionMatrix);
            DrawLockerDoor(viewMatrix, projectionMatrix);
            textSwirl.Draw(viewMatrix, projectionMatrix);
        }

        void DrawLocker(Matrix viewMatrix, Matrix projectionMatrix)
        {
            Matrix[] transforms = new Matrix[locker.Bones.Count];
            locker.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in locker.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;
                    effect.Texture = lockerTexture;
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(position);
                }
                mesh.Draw();
            }
        }

        void DrawLockerDoor(Matrix viewMatrix, Matrix projectionMatrix)
        {
            Matrix[] transforms = new Matrix[lockerDoor.Bones.Count];
            lockerDoor.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in lockerDoor.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;
                    effect.Texture = lockerDoorTexture;
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(MathHelper.ToRadians(rotationY)) * Matrix.CreateTranslation(position + doorOffset);
                }
                mesh.Draw();
            }
        }
    }
}
